package com.TinyBBS.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.TinyBBS.bean.User;
import com.TinyBBS.db.DBCRUD;

public class UserModel {
	private DBCRUD crud = null;
	private ResultSet rs = null;

	public UserModel() {
		crud = new DBCRUD();
	}

	/**
	 * get all record from user table
	 * 
	 * @return List<User>
	 */
	public List<User> getAllUser() {
		List<User> userList = new ArrayList<User>();
		try {
			rs = crud.fetchAll("user");
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setFirstname(rs.getString("firstname"));
				user.setLastname(rs.getString("lastname"));
				user.setEmail(rs.getString("email"));
				user.setPassword(rs.getString("password"));
				user.setStartdate(rs.getTimestamp("startdate"));
				user.setToken(rs.getString("token"));
				user.setAccess_level(rs.getInt("access_level"));
				userList.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userList;
	}

	/**
	 * Insert record by using User bean
	 * 
	 * @param user
	 *            User bean parameter
	 * @return if inserted successfully return true,else return false
	 */
	public boolean insertUser(User user) {
		Map<String, Object> mp = new HashMap<String, Object>();
		mp.put("username", user.getUsername());
		mp.put("firstname", user.getFirstname());
		mp.put("lastname", user.getLastname());
		mp.put("password", user.getPassword());
		mp.put("email", user.getEmail());
		mp.put("token", user.getToken());
		mp.put("startdate", user.getStartdate());
		mp.put("access_level", user.getAccess_level());

		if (crud.insert("user", mp)) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * Update user's password by email
	 * @param password Password
	 * @param email Email
	 * @return if update successfully return true else return false
	 */
	public boolean updatePassword(String password,User user){
		Map<String, Object> mp = new HashMap<String, Object>();
		mp.put("password", password);
		String where = "email = '" + user.getEmail() + "'"; 
		if(crud.update("user", mp, where)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Update data for user table
	 * @param maplist Data saved in Map
	 * @param username Username 
	 * @return if update successfully return true else return false
	 */
	public boolean updateData(Map<String, Object> maplist,String username){
		String where = "username = '"+ username + "'";
		if(crud.update("user", maplist, where)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Get User record by username
	 * 
	 * @param username
	 *            Username
	 * @return if Username exist in table return this user, return null if not
	 *         exist
	 */
	public User getUserByUsername(String username) {
		User user = new User();
		rs = crud.fetchAll("user", "username = " + "'" + username + "'");
		return this.setUserbyResultSet(user, rs);
	}

	/**
	 * Get User record by email
	 * @param email Email
	 * @return if Email exist in table return this user, return null if not
	 *         exist
	 */
	public User getUserByEmail(String email) {
		User user = new User();
		rs = crud.fetchAll("user", "email = " + "'" + email + "'");
		return this.setUserbyResultSet(user, rs);
	}
	/**
	 * verify user by email and password
	 * @param email
	 * @param password
	 * @return
	 */
	public User verifyUser(String email,String password){
		User user = new User();
		rs = crud.fetchAll("user", "email = '"+email+ "' and password = '"+password+"'");
		return this.setUserbyResultSet(user, rs);
	}

	/**
	 * Assign ResultSet to User bean
	 * 
	 * @param user
	 * @param rs
	 * @return User
	 */
	private User setUserbyResultSet(User user, ResultSet rs) {
		try {
			if (rs.next()) {
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setFirstname(rs.getString("firstname"));
				user.setLastname(rs.getString("lastname"));
				user.setEmail(rs.getString("email"));
				user.setPassword(rs.getString("password"));
				user.setStartdate(rs.getTimestamp("startdate"));
				user.setToken(rs.getString("token"));
				user.setAccess_level(rs.getInt("access_level"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return user;
	}
	/**
	 * Remove user
	 * @param id
	 * @return boolean true if deleted successfully, else false
	 */
	public boolean removeUser(String id){
		return crud.delete("user", "id = " + "'" + id + "'");
	}
}
